﻿using System;
using System.Linq;
using Biz.Buffer;
using Biz.EF.Platform.Kq;

namespace Biz.Process
{
    public static class ProcessorAlarm
    {
        #region feature
        private static string featureMonthGt80 = "本月已排污水占允许流量百分比超过80%";
        private static string featureMonthGt80Txt = "本月已排污水占允许流量百分比为{0},超过了80%!";
        private static string featureMonthGt90 = "本月已排污水占允许流量百分比超过90%";
        private static string featureMonthGt90Txt = "本月已排污水占允许流量百分比为{0},超过了90%!";
        private static string featureMonthGt100 = "本月已排污水超过允许排放流量";
        private static string featureMonthGt100Txt = "本月已排污水占允许流量百分比为{0},月已排污水超过允许排放流量!";
        private static string featureQuarterGt80 = "本季已排污水占允许流量百分比超过80%";
        private static string featureQuarterGt80Txt = "本季已排污水占允许流量百分比为{0},超过了80%!";
        private static string featureQuarterGt90 = "本季已排污水占允许流量百分比超过90%";
        private static string featureQuarterGt90Txt = "本季已排污水占允许流量百分比为{0},超过了90%!";
        private static string featureQuarterGt100 = "本季已排污水超过允许排放流量";
        private static string featureQuarterGt100Txt = "本季已排污水占允许流量百分比为{0},季已排污水超过允许排放流量!";
        private static string featureYearGt80 = "本年已排污水占允许流量百分比超过80%";
        private static string featureYearGt80Txt = "本年已排污水占允许流量百分比为{0},超过了80%!";
        private static string featureYearGt90 = "本年已排污水占允许流量百分比超过90%";
        private static string featureYearGt90Txt = "本年已排污水占允许流量百分比为{0},超过了90%!";
        private static string featureYearGt100 = "本年已排污水超过允许排放流量";
        private static string featureYearGt100Txt = "本年已排污水占允许流量百分比为{0},年已排污水超过允许排放流量!";
        #endregion
        public static void ProcessB01EmissionRatio(PlatformYgContext pCtx, Envi_PS_Waste_Water_Let_Port port)
        {
            var theLastFlow =
                pCtx.Envi_PS_Waste_Water_Flow.Where(t => t.Pollution_Source_ID == port.Pollution_Source_Id)
                    .Where(t => t.Let_Port_ID == port.ID)
                    .OrderByDescending(t => t.Monitor_Time)
                    .FirstOrDefault();
            foreach (AlarmType item in Enum.GetValues(typeof(AlarmType)))
            {
                GenB01MonthEmissionRatioWarn(pCtx, port, theLastFlow, item);
            }
        }
        private static void GenB01MonthEmissionRatioWarn(PlatformYgContext pCtx, Envi_PS_Waste_Water_Let_Port port, Envi_PS_Waste_Water_Flow theLastFlow, AlarmType type)
        {
            var percent = theLastFlow.Let_Per;
            decimal low = 80;
            decimal high = 90;
            string featureType = featureMonthGt80;
            string featureTxt = featureMonthGt80Txt;
            switch (type)
            {
                case AlarmType.MonthGtEighty:
                    low = 80;
                    high = 90;
                    featureType = featureMonthGt80;
                    featureTxt = featureMonthGt80Txt;
                    percent = theLastFlow.Let_Per;
                    break;
                case AlarmType.MonthGtNinety:
                    low = 90;
                    high = 100;
                    featureType = featureMonthGt90;
                    featureTxt = featureMonthGt90Txt;
                    percent = theLastFlow.Let_Per;
                    break;
                case AlarmType.MonthGtHundred:
                    low = 90;
                    high = decimal.MaxValue;
                    featureType = featureMonthGt100;
                    featureTxt = featureMonthGt100Txt;
                    percent = theLastFlow.Let_Per;
                    break;
                case AlarmType.QuarterGtEighty:
                    low = 80;
                    high = 90;
                    featureType = featureQuarterGt80;
                    featureTxt = featureQuarterGt80Txt;
                    percent = theLastFlow.Quarter_Let_Per;
                    break;
                case AlarmType.QuarterGtNinety:
                    low = 90;
                    high = 100;
                    featureType = featureQuarterGt90;
                    featureTxt = featureQuarterGt90Txt;
                    percent = theLastFlow.Quarter_Let_Per;
                    break;
                case AlarmType.QuarterGtHundred:
                    low = 100;
                    high = Decimal.MaxValue;
                    featureType = featureQuarterGt100;
                    featureTxt = featureQuarterGt100Txt;
                    percent = theLastFlow.Quarter_Let_Per;
                    break;
                case AlarmType.YearGtEighty:
                    low = 80;
                    high = 90;
                    featureType = featureYearGt80;
                    featureTxt = featureYearGt80Txt;
                    percent = theLastFlow.Year_Let_Per;
                    break;
                case AlarmType.YearGtNinety:
                    low = 90;
                    high = 100;
                    featureType = featureYearGt90;
                    featureTxt = featureYearGt90Txt;
                    percent = theLastFlow.Year_Let_Per;
                    break;
                case AlarmType.YearGtHundred:
                    low = 100;
                    high = Decimal.MaxValue;
                    featureType = featureYearGt100;
                    featureTxt = featureYearGt100Txt;
                    percent = theLastFlow.Year_Let_Per;
                    break;

            }
            if (theLastFlow.Monitor_Time == null || percent == null) return;
            var telephone =
                pCtx.Envi_PS_Enterprise_M_Telephone
                    .FirstOrDefault(t => t.Pollution_Source_ID == port.Pollution_Source_Id);

            if (percent < high && percent.Value >= low)
            {
                var year = theLastFlow.Monitor_Time.Value.Year;
                var month = theLastFlow.Monitor_Time.Value.Month;
                var record = pCtx.T_GSM_warn
                    .Where(t => t.Let_Port_Id == theLastFlow.Let_Port_ID)
                    .Where(t => t.txtDate != null && t.txtDate.Value.Year == year && t.txtDate.Value.Month == month)
                    .Where(t => t.alarmType.Equals(featureType))
                    .OrderByDescending(t => t.txtDate)
                    .FirstOrDefault();
                if (record == null)
                {
                    var gsm1 = new T_GSM_warn
                    {
                        Let_Port_Id = (int?)theLastFlow.Let_Port_ID,
                        txt = string.Format(featureTxt, percent),
                        alarmType = featureType,
                        Flag = "0",
                        MN = CommonInformation.GetLetportById((int?)theLastFlow?.Let_Port_ID)?.Data_Collection_Instrument_SN,
                        txtDate = theLastFlow.Monitor_Time,
                        Pollution_Source_Name = theLastFlow.Pollution_Source_Name,
                        ReceiverPersonName = telephone?.Principal_Name,
                        Tel = telephone?.Principa_M_Telephone
                    };
                    pCtx.T_GSM_warn.Add(gsm1);
                    var gsm2 = new T_GSM_warn
                    {
                        Let_Port_Id = (int?)theLastFlow?.Let_Port_ID,
                        txt = string.Format(featureTxt, percent),
                        alarmType = featureType,
                        Flag = "0",
                        MN = CommonInformation.GetLetportById((int?)theLastFlow?.Let_Port_ID)?.Data_Collection_Instrument_SN,
                        txtDate = theLastFlow.Monitor_Time,
                        Pollution_Source_Name = theLastFlow.Pollution_Source_Name,
                        ReceiverPersonName = telephone?.Envi_Part_Leader_Name,
                        Tel = telephone?.Envi_Part_Leader_M_Telephone
                    };
                    pCtx.T_GSM_warn.Add(gsm2);
                    var gsm3 = new T_GSM_warn
                    {
                        Let_Port_Id = (int?)theLastFlow?.Let_Port_ID,
                        txt = string.Format(featureTxt, percent),
                        alarmType = featureType,
                        Flag = "0",
                        MN = CommonInformation.GetLetportById((int?)theLastFlow?.Let_Port_ID)?.Data_Collection_Instrument_SN,
                        txtDate = theLastFlow.Monitor_Time,
                        Pollution_Source_Name = theLastFlow.Pollution_Source_Name,
                        ReceiverPersonName = telephone?.Envi_Dept_Principa_Name,
                        Tel = telephone?.Envi_Dept_Principa_M_Telephone
                    };
                    pCtx.T_GSM_warn.Add(gsm3);
                    var gsm4 = new T_GSM_warn
                    {
                        Let_Port_Id = (int?)theLastFlow?.Let_Port_ID,
                        txt = string.Format(featureTxt, percent),
                        alarmType = featureType,
                        Flag = "0",
                        MN = CommonInformation.GetLetportById((int?)theLastFlow?.Let_Port_ID)?.Data_Collection_Instrument_SN,
                        txtDate = theLastFlow.Monitor_Time,
                        Pollution_Source_Name = theLastFlow.Pollution_Source_Name,
                        ReceiverPersonName = telephone?.Envi_Operator_Name,
                        Tel = telephone?.Envi_Operator_M_Telephone
                    };
                    pCtx.T_GSM_warn.Add(gsm4);
                    if (type == AlarmType.MonthGtHundred || type == AlarmType.QuarterGtHundred ||
                        type == AlarmType.YearGtHundred)
                    {
                        var hbjTelephone = pCtx.Envi_PS_Note_SetNumber.FirstOrDefault(t => t.Pollution_Source_Id == port.Pollution_Source_Id);
                        if (hbjTelephone?.Hbj_Duty_Man != null)
                        {
                            var ids = hbjTelephone.Hbj_Duty_Man.Split(',');
                            foreach (var id in ids)
                            {
                                try
                                {
                                    var idInt = int.Parse(id);
                                    var communication =
                                        CommonInformation.GetAllCommunications().FirstOrDefault(t => t.cId == idInt);
                                    if (communication != null)
                                    {
                                        var hour = DateTime.Now.Hour;
                                        if (hour > 8 && hour < 22)
                                        {
                                            var gsm5 = new T_GSM_warn
                                            {
                                                Let_Port_Id = (int?) theLastFlow?.Let_Port_ID,
                                                txt = string.Format(featureTxt, percent),
                                                alarmType = featureType,
                                                Flag = "0",
                                                MN =
                                                    CommonInformation.GetLetportById((int?) theLastFlow?.Let_Port_ID)?
                                                        .Data_Collection_Instrument_SN,
                                                txtDate = theLastFlow.Monitor_Time,
                                                Pollution_Source_Name = theLastFlow.Pollution_Source_Name,
                                                ReceiverPersonName = communication.Principal_Name,
                                                Tel = communication.Principal_Phone
                                            };
                                            pCtx.T_GSM_warn.Add(gsm5);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {


                                }
                            }
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
        }
    }

    public enum AlarmType
    {
        MonthGtEighty,
        MonthGtNinety,
        MonthGtHundred,
        QuarterGtEighty,
        QuarterGtNinety,
        QuarterGtHundred,
        YearGtEighty,
        YearGtNinety,
        YearGtHundred
    }
}
